-- weaken
-- created by panyl
-- 通用的削弱目标类技能

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 如果免疫了该技能
        if SkillM.immunitySkill(source, target, skillId) then
            if FormulaM.invoke("CAN_TARGET_INJURED", target, skillId) then
                -- 如果不是无敌状态，则播放免疫的效果
                SkillM.getSequence(target):immunity(source, target, skillId);
            end
            return {};
        end

        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[5] > 0 then
            effectRound = round + para[5] + 1;
        end

        -- 属性：持续时间+
        local addonRound = 0;
        local prop = PropM.combine(source, "magic_last", skillId);
        addonRound = PropM.apply(prop, addonRound);
        prop = PropM.combine(source, "magic_last", "assist");
        addonRound = PropM.apply(prop, addonRound);
        prop = PropM.combine(source, "magic_last", "all");
        addonRound = PropM.apply(prop, addonRound);

        -- 如果是boss，减半
        if target:isBoss() then
            effectRound = round + 1 + 1;
            addonRound  = extraCeil(addonRound, 2);
        end
        effectRound = effectRound + addonRound;

        -- 魔力
        local magic = source:getMagic();
        -- 被对方百分比削弱
        prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 威力
        local force = para[4];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        force = math.modf(force * (addon_rate + 1000)/1000);

        local v = math.modf(force * magic / 1000);

        v = math.max(1, v);

        -- 增加命中动作
        --SkillM.getSequence():hit(source, target, skillId);

        local condition = {
            ["id"] = para[2],
            ["value"] = v,
            ["end_round"] = effectRound,
        };

        -- 附加状态
        CombatStatusM.applyStatus(target, para[1], condition);

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), arr[3], tonumber(arr[4]), tonumber(arr[5] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 魔力
        local magic = source:getMagic();

        -- 威力
        local base = para[4];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 效果的显示
        local vbase = math.modf(base * magic / 1000);
        local vforce = math.modf(force * magic / 1000);
        vbase = math.max(1, vbase);
        vforce = math.max(1, vforce);
        local vaddon = vforce - vbase;
        if vaddon == 0 then
            desc = string.gsub(desc, "{weaken}", vforce);
        else
            desc = string.gsub(desc, "{weaken}", vbase .. "(+" .. vaddon .. ")");
        end

        local v = math.modf(force * magic / 1000);

        -- 回合数的显示
        local baseRound = para[5];
        local round = para[5];
        -- 增加持续时间的效果
        local prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        -- BOSS及PVP?的回合数显示
        baseRound = 1;
        round = 1;

        -- 增加持续时间的效果
        prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        -- 持续时间+属性，boss需要减半
        addonRound = round - baseRound;
        addonRound = extraCeil(addonRound, 2);

        if addonRound == 0 then
            desc = string.gsub(desc, "{roundBoss}", baseRound);
        else
            desc = string.gsub(desc, "{roundBoss}", baseRound .. "(+" .. addonRound .. ")");
        end

        return desc;
    end,
};
